﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace Axium.Services.Hosting.Authentication
{
    /// <summary>
    /// This this role provider to autoficate user. Just one methos IsUserInRole is used for windows users
    /// It can be replaced by SqlRoleProvider
    /// </summary>
    public class AxiumRoleProvider : RoleProvider
    {
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName { get; set; }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            //this needs to be returned for specific user. It is not called for Windows users (internal service)
            if (username == "tadmin") return new[] { "Administrators" };
            return new[] {"user"};

        }
        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            //due to we use one service (two endpoints) we need to share one RoleProvider
            //this authorazation will be used for both interna and extarnal users.
            //Here is harcode for "test_admin". it will be administrator
            if (username == "test_admin" && roleName == "Administrators") return true;
            //any users from domain will be treated as Administrators for testing
            if (username.Contains("\\")) return true;
            //any user in any groups
            if (username != "test_admin" && roleName != "Administrators") return true;
            return false;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
    }
}